﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GeFIN
{
    partial class LogInMenu : Form
    {
        //conexão
        Connection connection;

        public LogInMenu()
        {
            InitializeComponent();
            connection = new Connection();
        }

        private void panelExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'geFINSystemDataSet.Conta' table. You can move, or remove it, as needed.
            this.contaTableAdapter.Fill(this.geFINSystemDataSet.Conta);
        }

        private void panelEnter_Click(object sender, EventArgs e)
        { 
            //Comando de consulta usuario
            string consultaUser = "select usuario from Conta where usuario = '" + cbUser.Text + "'";
            connection.consult(consultaUser);
            bool isUserValid = connection.readConsult();
            connection.finishConsult();

            //Comando de consulta senha
            string consultaPassword = "select senha from Conta where usuario = '" + cbUser.Text + "'";
            connection.consult(consultaPassword);
            bool isPasswordValid = connection.readConsult();
            string password = "";
            if(isPasswordValid) password = connection.getResultString(0);
            connection.finishConsult();

            if (!isUserValid)
            {
                MessageBox.Show(this, "O nome de usuário dado não está cadastrado.", "Usuário inválido", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (tbPassword.Text == "")
            {
                MessageBox.Show(this, "Para logar, entre com a senha da sua conta!", "Digite as senha", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (tbPassword.Text != password)
            {
                MessageBox.Show(this, "A senha está incorreta.Tente outra vez", "Senha inválida", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else 
            {
                this.Close();
            }

        }

        private void panelForgotPassword_Click(object sender, EventArgs e)
        {
            if (labelForgotPassword.Visible & labelDica.Visible)
            {
                labelDica.Visible = false;
                labelForgotPassword.Visible = false;
            }
            else
            {
                labelDica.Visible = true;
                labelForgotPassword.Visible = true;
            }
        }

        private void cbUser_TextChanged(object sender, EventArgs e)
        {
            //comando de consulta user
            string consultaUser = "select usuario from Conta where usuario = '" + cbUser.Text + "'";
            connection.consult(consultaUser);
            bool isUserValid = connection.readConsult();
            connection.finishConsult();

            //comando de consulta dica
            string consultaDica = "select dica from Conta where usuario = '" + cbUser.Text + "'";
            connection.consult(consultaDica);
            bool isDicaValid = connection.readConsult();
            string dica = "";
            if(isDicaValid) dica = connection.getResultString(0);
            connection.finishConsult();

            if (isUserValid)
            {
                labelForgotPassword.Text = dica;
                //labelDica.Visible = true;
                //labelForgotPassword.Visible = true;
            }
            else
            {
                labelForgotPassword.Text = "";
                //labelDica.Visible = true;
                //labelForgotPassword.Visible = true;
            }
        }

        private void panelSignIn_Click(object sender, EventArgs e)
        {
            cadastroConta formCadastro = new cadastroConta();
            formCadastro.exibirInclusao(contaBindingSource.DataSource, contaBindingSource.DataMember);

            if (formCadastro.DialogResult == DialogResult.OK) contaTableAdapter.Update(geFINSystemDataSet.Conta);            
        }

        public DialogResult showDialog(int bindingPosition)
        {
            this.contaBindingSource.Position = bindingPosition;
            return this.ShowDialog();
        }

        public void disconnect()
        {
            this.connection.close();
        }
    }
}
